#!/bin/bash -e
# Copyright (c) Contributors to the Apptainer project, established as
#   Apptainer a Series of LF Projects LLC.
#   For website terms of use, trademark policy, privacy policy and other
#   project policies see https://lfprojects.org/policies

# Run docker as shown at
#  https://djw8605.github.io/2016/05/03/building-centos-packages-on-travisci/
#
# This more complicated setup is needed for github actions too because 
#  they do not provide a mechanism for directly using docker --privileged,
#  which is needed for any reasonable subset of apptainer tests to succeed
#  (it is not needed for building, just for testing).
# Github actions does at least start a VM with docker already running.

DOCKER_HUB_URI="$OS_TYPE:$OS_VERSION"
docker pull "$DOCKER_HUB_URI"

DOCKER_CONTAINER_NAME="test_${OS_TYPE##*/}_${OS_VERSION//./_}"
PKGTYPE=rpm
if [ "$OS_TYPE" = "debian" ] || [ "$OS_TYPE" = "ubuntu" ]; then
    PKGTYPE=deb
fi

CONTAINER_VERS="${CONTAINER_VERS:-$DOCKER_HUB_URI}"

docker run --privileged --network=host -v "$(pwd):/build:rw" \
  -e OS_TYPE=$OS_TYPE -e GO_ARCH=$GO_ARCH -e CONTAINER_VERS="$CONTAINER_VERS" \
  -e HIDE_DIST=$HIDE_DIST -e INS_OPTS="$INS_OPTS" \
  -e GITHUB_TOKEN="$GITHUB_TOKEN" \
  --name "$DOCKER_CONTAINER_NAME" "$DOCKER_HUB_URI" /bin/bash -exc \
	"cd /build && scripts/ci-${TEST_TYPE:-$PKGTYPE-build}-test"

docker ps -a
docker stop "$DOCKER_CONTAINER_NAME"
docker rm -v "$DOCKER_CONTAINER_NAME"
